// TODO:  财务报表审核
import React, { VFC, useState, useEffect} from "react"
import {Modal, Form, Input, Button, message, Space, Table  } from 'antd';
import { useHistory } from "react-router-dom";;

import {  getTeamFinance, setTeamComment, getTeamComment } from '@services/index';
import NoInfo from '@components/noInfo';

const FormItem = Form.Item;
const { TextArea } = Input;

const Report:VFC<any> = props => {
    const {teamId, isVisible, onCancel} = props;
    const {location} = useHistory() as any
    const [teamProjectMoney, setTeamProjectMoney] = useState<any>({});
    const [dataList, setDataList] = useState<any[]>([]);
    const [singleMoney, setSingleMoney] = useState<any>({});
    const [isShow, setIsShow] = useState<boolean>(false);
    const [loading, setLoading] = useState<boolean>(false)
    const [comment, setComment] = useState<any>(undefined)
    const params:any = {
        phaseId: location.state?.phaseId,
        teamId,
        projectId: JSON.parse(sessionStorage.userInfo).projectId
    }
    useEffect(()=>{
        init()
    }, [])
    const init = () => {
        setLoading(true)
        getTeamFinance(params).then(({status, data}) => {
            if(status === '0'){
                let _isShow = Object.keys(data).length>0;
                setIsShow(_isShow);
                const {teamFinance, teamProjectMoney} = data;
                let obj:any = {}
                let newList:any = []
                teamFinance.map((item: any) => {
                    obj[item.type] = []
                    return item
                }).forEach((item: any) => {
                    obj[item.type].push(item)
                })
                let moneyObj = {}
                Object.keys(obj).map(key=>{
                    obj[key].map((item:any, index:number)=>{
                        moneyObj[`sum_money${key}`] = moneyObj[`sum_money${key}`]?moneyObj[`sum_money${key}`]+item.sum_money:item.sum_money
                        newList[index] = {
                            ...newList[index],
                            [`money${key}`]: item.type=='1'||item.type=='2'||item.type=='4'?filterMoney(item.money):filterMoney(item.sum_money),
                            [`day${key}`]: item.day,
                            [`num${key}`]: item.sum_money!=0?item.num:'',
                            [`name${key}`]: item.remark,
                            index: index+1
                        }
                    })
                })
                setSingleMoney(moneyObj)
                setDataList(newList);
                setTeamProjectMoney(teamProjectMoney);
            }
        }).finally(()=>setLoading(false))
        // 回显
        if(location.state?.phaseId == 7){
            getTeamComment(params).then(({status, data})=>{
                if(status === '0'){
                    const {list} = data
                    setComment(list?.length&&list[0].comment)
                }
            })
        }
    }
    
    // 提交表单数据
    const submit = async (type: string) => {
        if(!comment){
            message.error('请输入评语');
            return
        }
        // 0 未评论 1 通过 2 未通过
        params.comment = comment
        params.pass = type=='Y' ? 1: 2
        const { status } = await setTeamComment(params)
        if(status == '0'){
            message.success('提交成功');
            onCancel()
        }
    };
    // 金额转换成 万
    const filterMoney = (val:number) => {
        return val ? val/10000 +'w' : '';
    }
    const columns = [
        {
            title: '现金流出',
            children: [
                {
                    title: '序号',
                    dataIndex: 'index',
                    width: 90,
                    align: 'center'
                },
                {
                    title: '工资支出',
                    children: [
                        {
                            title: '单价(每人每天)',
                            dataIndex: 'money1'
                        },
                        {
                            title: '人数',
                            dataIndex: 'num1'
                        },
                        {
                            title: '天数',
                            dataIndex: 'day1'
                        }
                    ]
                },
                {
                    title: '购买服务',
                    children: [
                        {
                            title: '服务名称',
                            dataIndex: 'name2'
                        },
                        {
                            title: '单价',
                            dataIndex: 'money2'
                        },
                        {
                            title: '数量',
                            dataIndex: 'num2'
                        }
                    ],
                },
                {
                    title: '购买产品/设备',
                    children: [
                        {
                            title: '探测器',
                            dataIndex: 'name3'
                        },
                        {
                            title: '价格',
                            dataIndex: 'money3'
                        }
                    ],
                },
                {
                    title: '保险费用',
                    children: [
                        {
                            title: '保险名称',
                            dataIndex: 'name4'
                        },
                        {
                            title: '单价',
                            dataIndex: 'money4'
                        },
                        {
                            title: '数量',
                            dataIndex: 'num4'
                        }
                    ],
                }
            ]
        },
        {
            title: '现金流入',
            children: [
                {
                    title: '拓展项目收入',
                    children: [
                        {
                            title: '项目名称',
                            dataIndex: 'name5'
                        },
                        {
                            title: '金额',
                            dataIndex: 'money5'
                        }
                    ]
                },
                {
                    title: '销售成果',
                    children: [
                        {
                            title: '成果产品名称',
                            dataIndex: 'name6'
                        },
                        {
                            title: '销售价格',
                            dataIndex: 'money6'
                        }
                    ],
                },
            ],
        }
    ];
    return (
        <Modal title={'财务报表'} width={1290} visible={isVisible} onCancel={onCancel} footer={false}>
            {isShow?
            <Table columns={columns} rowKey={({index})=>index} dataSource={dataList} loading={loading} bordered pagination={false}
                summary={()=>(
                <Table.Summary fixed>
                    <Table.Summary.Row>
                    <Table.Summary.Cell index={0}>单项总额</Table.Summary.Cell>
                    <Table.Summary.Cell index={1} colSpan={3}>{filterMoney(singleMoney.sum_money1)}</Table.Summary.Cell>
                    <Table.Summary.Cell index={2} colSpan={3}>{filterMoney(singleMoney.sum_money2)}</Table.Summary.Cell>
                    <Table.Summary.Cell index={3} colSpan={2}>{filterMoney(singleMoney.sum_money3)}</Table.Summary.Cell>
                    <Table.Summary.Cell index={4} colSpan={3}>{filterMoney(singleMoney.sum_money4)}</Table.Summary.Cell>
                    <Table.Summary.Cell index={5} colSpan={2}>{filterMoney(singleMoney.sum_money5)}</Table.Summary.Cell>
                    <Table.Summary.Cell index={6} colSpan={2}>{filterMoney(singleMoney.sum_money6)}</Table.Summary.Cell>
                    </Table.Summary.Row>
                    <Table.Summary.Row>
                    <Table.Summary.Cell index={0}>期初现金</Table.Summary.Cell>
                    <Table.Summary.Cell index={1} colSpan={15}>{filterMoney(teamProjectMoney.totalMoney)}</Table.Summary.Cell>
                    </Table.Summary.Row>
                    <Table.Summary.Row>
                    <Table.Summary.Cell index={0}>期末现金</Table.Summary.Cell>
                    <Table.Summary.Cell index={1} colSpan={15}>{filterMoney(teamProjectMoney.balance)}</Table.Summary.Cell>
                    </Table.Summary.Row>
                </Table.Summary>
                )}
            />
            :
            <NoInfo />
            }
            {location.state?.phaseId == 7 && <>
                <Form style={{marginTop:20}}>
                    <FormItem label="评语">
                        <TextArea  value={comment} onChange={e => setComment(e.target.value)} placeholder="请输入评语"/>
                    </FormItem>
                </Form>
                <Space size={20} style={{display: 'flex',justifyContent: 'center'}}>
                    <Button type="primary" onClick={()=>submit('Y')}>通过</Button>
                    <Button onClick={()=>submit('N')}>不通过</Button>
                </Space>
            </>
            }
        </Modal>
    )
}
export default Report
